//
//  OKLOLViewController.m
//  ZLFastDoctor
//
//  Created by answer on 16/5/27.
//  Copyright © 2016年 LS. All rights reserved.
//
#import <MediaPlayer/MediaPlayer.h>
#import <AVFoundation/AVFoundation.h>
#import "OKLOLViewController.h"
#import "OKLOLViewCell.h"
#import "OKLOLModel.h"
#import "OKMovieViewController.h"

@interface OKLOLViewController () <UICollectionViewDelegate, UICollectionViewDataSource>

@property(nonatomic, strong) UICollectionViewFlowLayout * flowLayout;
@property(nonatomic, strong) NSArray * data;
@end

@implementation OKLOLViewController

static NSString * const reuseIdentifier = @"Cell";

- (instancetype)init
{
    self.title = @"王者组名医直播LOL";
    
    self = [super initWithCollectionViewLayout:self.flowLayout];
    if (self) {
        
    }
    return self;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.collectionView.backgroundColor = [UIColor whiteColor];
    // 设置flowlayout相关属性
    self.flowLayout.itemSize = CGSizeMake(self.view.bounds.size.width - 20, 238);
    
    self.collectionView.contentInset = UIEdgeInsetsMake(10, 0, 0, 0);
    
    // 注册cell
    [self.collectionView registerClass:[OKLOLViewCell class] forCellWithReuseIdentifier:reuseIdentifier];
    [self reloadMediaData];
}

- (void)reloadMediaData {
    [OKLOLModel requestLOLcompletion:^(NSArray *result) {
        self.data = result;
//        NSLog(@"%@",result);
        [self.collectionView reloadData];
    }];
}

#pragma mark <UICollectionViewDataSource>

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.data.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    OKLOLViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    
    // cell 背景颜色
    cell.backgroundColor = NQColor(208, 215, 240);
    // cell边框
    cell.layer.borderWidth = 5;
    cell.layer.borderColor = [UIColor grayColor].CGColor;
    // 圆角
    cell.layer.cornerRadius = 5;
    // 阴影颜色
    cell.layer.shadowColor = NQColor(104, 113, 205).CGColor;
    cell.layer.shadowOffset = CGSizeMake(5, 5);
    cell.layer.shadowOpacity = 1.0;
    
    cell.model = self.data[indexPath.item];
    
    return cell;
}



- (UICollectionViewFlowLayout *)flowLayout {
    
    if (_flowLayout == nil) {
        _flowLayout = [[UICollectionViewFlowLayout alloc]init];
    }
    return _flowLayout;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    OKLOLModel *model = self.data[indexPath.item];
    NSString *urlString = [NSString stringWithFormat:@"http://hls.quanmin.tv/live/%@/playlist.m3u8", model.uid];
    NSURL *url = [NSURL URLWithString:urlString];
    
    MPMoviePlayerViewController *mpvc = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
    [self presentMoviePlayerViewControllerAnimated:mpvc];
}

@end






